package com.riko.comic.module.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.riko.comic.module.sys.bean.po.Collect;
import com.riko.comic.module.sys.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wudechuang
 * @since 2021-11-01
 */
@Controller
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private ICollectService collectService;

    @RequestMapping("/init")
    @ResponseBody
    public boolean initCol(HttpServletRequest request, Integer comicId){
        Integer userId = (Integer)request.getSession().getAttribute("loginUserId");

        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("comic_id", comicId);
        Collect collect = collectService.getOne(wrapper);
        if(collect == null){
            return false;
        } else {
            return true;
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public String saveCollection(HttpServletRequest request, Integer comicId){
        Integer userId = (Integer)request.getSession().getAttribute("loginUserId");

        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setComicId(comicId);
        collect.setCollectTime(LocalDateTime.now());

        collectService.save(collect);

        return null;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delCollection(HttpServletRequest request, Integer comicId){
        Integer userId = (Integer)request.getSession().getAttribute("loginUserId");

        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setComicId(comicId);

        collectService.delByIds(collect);

        return null;
    }
}
